﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WordCloudInterface
{
    public partial class                        RecordFromMicrophone : Form
    {
        #region Attributes

        //
        // Constants
        //
        private readonly double                 SPEECH_RECOGNITION_CONFIDENCE;
        private readonly int                    CHUNK_SIZE;
        private readonly int                    RATE;
        private readonly int                    BITS;
        
        //
        // Parent form
        //
        private Interface                       parentForm;

        //
        // Attributes
        //
        private MicrophoneRecorder              recorder;

        #endregion

        #region Constructor

        public                                  RecordFromMicrophone(Interface ParentForm,
                                                                        double SpeechRecognitionConfidence = 0.0,
                                                                        int ChunkSize = 800000,
                                                                        int Rate = 16000,
                                                                        int Bits = 16)
        {
            this.parentForm = ParentForm;
            this.SPEECH_RECOGNITION_CONFIDENCE = SpeechRecognitionConfidence;
            this.CHUNK_SIZE = ChunkSize;
            this.RATE = Rate;
            this.BITS = Bits;

            InitializeComponent();

            // Change window upper-left icon
            this.Icon = Interface.GetProgramIcon();

            this.btnRefreshSrc.Click += new System.EventHandler(this.btnRefreshSources_Click);
            this.btnRecord.Click += new System.EventHandler(this.btnStartRecord_Click);
            this.btnStop.Click += new System.EventHandler(this.btnStopRecord_Click);
        }

        #endregion

        #region Button events

        private void                            btnRefreshSources_Click(object sender, System.EventArgs e)
        {
            List<Tuple<string, string>> sources = MicrophoneRecorder.GetSources();
            this.sourceList.Items.Clear();
            foreach (Tuple<string, string> src in sources)
            {
                ListViewItem item = new ListViewItem(src.Item1);
                item.SubItems.Add(new ListViewItem.ListViewSubItem(item, src.Item2));
                this.sourceList.Items.Add(item);
            }
        }

        private void                            btnStopRecord_Click(object sender, System.EventArgs e)
        {
            if (this.recorder == null)
                return;

            this.recorder.StopRecording();
            this.recorder = null;
        }

        private void                            btnStartRecord_Click(object sender, System.EventArgs e)
        {
            if (this.recorder != null)
                return;
            if (this.sourceList.SelectedItems.Count == 0)
                return;

            this.recorder = new MicrophoneRecorder(this.parentForm, SPEECH_RECOGNITION_CONFIDENCE, CHUNK_SIZE, RATE, BITS);
            int deviceNumber = this.sourceList.SelectedItems[0].Index;
            this.recorder.StartRecording(deviceNumber);
        }

        #endregion

    }
}
